package com.cloud.redis7.utils;

import com.cloud.redis7.entities.Customer;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.entity.EntityTable;
import tk.mybatis.mapper.mapperhelper.EntityHelper;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

//@Component
public class SqlGenerationDebug implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        try {
            System.out.println("=== 开始调试SQL生成过程 ===");

            // 获取EntityTable
            EntityTable entityTable = EntityHelper.getEntityTable(Customer.class);

            System.out.println("表名: " + entityTable.getName());

            // 获取所有列 - 使用正确的类型 LinkedHashSet
            Set<EntityColumn> allColumns = entityTable.getEntityClassColumns();
            System.out.println("所有列数量: " + allColumns.size());
            System.out.println("所有列信息:");
            for (EntityColumn column : allColumns) {
                System.out.println("  - 属性: " + column.getProperty() +
                        ", 列名: " + column.getColumn() +
                        ", 是否主键: " + column.isId() +
                        ", JDBC类型: " + column.getJdbcType());
            }
            // 获取主键列 - 使用正确的方法名 getEntityClassPKColumns()
            Set<EntityColumn> pkColumns = entityTable.getEntityClassPKColumns();
            System.out.println("主键列数量: " + pkColumns.size());
            System.out.println("主键列信息:");
            for (EntityColumn column : pkColumns) {
                System.out.println("  - 属性: " + column.getProperty() +
                        ", 列名: " + column.getColumn() +
                        ", JDBC类型: " + column.getJdbcType());
            }

            // 模拟SELECT子句
            StringBuilder selectSql = new StringBuilder();
            selectSql.append("SELECT ");
            boolean first = true;
            for (EntityColumn column : allColumns) {
                if (!first) selectSql.append(", ");
                selectSql.append(column.getColumn());
                first = false;
            }

            // 模拟WHERE子句（主键条件）
            StringBuilder whereSql = new StringBuilder();
            whereSql.append(" WHERE ");
            first = true;
            for (EntityColumn column : pkColumns) {
                if (!first) whereSql.append(" AND ");
                whereSql.append(column.getColumn());
                whereSql.append(" = ?");
                first = false;
            }

            // 模拟WHERE子句（所有字段条件 - 错误情况）
            StringBuilder whereAllSql = new StringBuilder();
            whereAllSql.append(" WHERE ");
            first = true;
            for (EntityColumn column : allColumns) {
                if (!first) whereAllSql.append(" AND ");
                whereAllSql.append(column.getColumn());
                whereAllSql.append(" = ?");
                first = false;
            }

            System.out.println("=== 生成的SQL ===");
            System.out.println("正确的主键查询SQL:");
            System.out.println(selectSql.toString() + " FROM " + entityTable.getName() + whereSql.toString());

            System.out.println("\n错误的全部字段查询SQL:");
            System.out.println(selectSql.toString() + " FROM " + entityTable.getName() + whereAllSql.toString());

            System.out.println("\n=== 问题分析 ===");
            if (pkColumns.size() == 1) {
                System.out.println("✓ 主键识别正确：只有一个主键字段");
            } else if (pkColumns.size() > 1) {
                System.out.println("✗ 主键识别错误：有多个字段被识别为主键");
            } else {
                System.out.println("✗ 主键识别错误：没有识别到主键字段");
            }

            System.out.println("当前实际执行的SQL是错误的那种，说明通用Mapper内部逻辑有问题");

        } catch (Exception e) {
            System.err.println("调试过程中发生错误:");
            e.printStackTrace();
        }
    }
}